﻿// See https://aka.ms/new-console-template for more information

using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using 昆仑通态报警导入;

Console.WriteLine("请输入Excel文件地址");

var filePath = Console.ReadLine();

#pragma warning disable CS8604 // 引用类型参数可能为 null。
var fileInfoExl = new FileInfo(filePath);
#pragma warning restore CS8604 // 引用类型参数可能为 null。

List<InPutTableRow> rows = new List<InPutTableRow>();   //exl表格转换的实体类列表
//读取exl文件
using (FileStream exlFile = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
    IWorkbook workBook = new XSSFWorkbook(exlFile);    //获取工作簿

    ISheet sheet = workBook.GetSheetAt(0);    //获取工作表

    for (int i = 1; i <= sheet.LastRowNum; i++)
    {
        IRow row = sheet.GetRow(i);
        InPutTableRow inputTableRow = new InPutTableRow();
        inputTableRow.AlarmName = row.GetCell(0).ToString();
        inputTableRow.AlarmText = row.GetCell(1).ToString();
        rows.Add(inputTableRow);
    }
}

#region 生成xml实体类
    var fileInfoXml = new FileInfo(@"../../../AlarmList.xml");

    var xs = new XmlSerializer(typeof(root));

    var alarmListOut = xs.Deserialize(new StreamReader(fileInfoXml.FullName)) as root;
#endregion

//数据输入
for (int i = 0; i < rows.Count; i++)
{
    alarmListOut.报警项[i].ID = (i+1).ToString();
    alarmListOut.报警项[i].变量名称 = rows[i].AlarmName;
    alarmListOut.报警项[i].变量类型 = "整数";
    alarmListOut.报警项[i].报警类型 = "正跳变";
    alarmListOut.报警项[i].可用性 = "1";
    alarmListOut.报警项[i].基准值 = "-";
    alarmListOut.报警项[i].触发误差 = "-";
    alarmListOut.报警项[i].解除误差 = "-";
    alarmListOut.报警项[i].报警分组 = "0";
    alarmListOut.报警项[i].报警级别 = "0";
    alarmListOut.报警项[i].多语言ID = "-1";
    alarmListOut.报警项[i].多语言类型名称 = "中文";
    alarmListOut.报警项[i].报警描述 = rows[i].AlarmText;
}

//生成XML文件
Console.WriteLine(fileInfoExl.Directory.ToString());

using (FileStream s = new FileStream(fileInfoExl.Directory +  @"\AlarmOut.xml", FileMode.Create, FileAccess.ReadWrite))
{
    XmlSerializerNamespaces _namespaces = new XmlSerializerNamespaces(new XmlQualifiedName[] { new XmlQualifiedName(string.Empty, "aa") });
    TextWriter writer = new StreamWriter(s, Encoding.UTF8);
    xs.Serialize(writer, alarmListOut);
}
Console.WriteLine("执行完成");
Console.ReadKey();